package com.leetcode.May;

import java.util.*;

/**
 * @Description: 字母异位词分组
 * @Author: Mr.Fxy
 * @CreateTime: 2024-05-10 08:00
 */
public class GroupOfAllogramWords {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String sorted = new String(charArray);
            if (!map.containsKey(sorted)) {
                map.put(sorted, new ArrayList<>());
            }
            map.get(sorted).add(str);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
        GroupOfAllogramWords anagrams = new GroupOfAllogramWords();
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = anagrams.groupAnagrams(strs);
        System.out.println(result);
    }
}
